﻿using System.Runtime.CompilerServices;
using UnityEngine;

namespace ProjectDanmaku.Util
{
    public static class MathHelper
    {
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static float Vec2ToRotateY(Vector2 vec)
        {
            vec.Normalize();
            
            var vDotU = Vector2.Dot(vec, Vector2.up);
            var right = Mathf.Sign(Vector2.Dot(vec, Vector2.right));

            return -right * Mathf.Acos(vDotU);
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static Vector2 RotateVec2(Vector2 v, float angleInDeg)
        {
            var angleInRad = angleInDeg * Mathf.Deg2Rad;
            
            float cosTheta = Mathf.Cos(angleInRad);
            float sinTheta = Mathf.Sin(angleInRad);

            // 计算旋转后的向量
            float newX = v.x * cosTheta - v.y * sinTheta;
            float newY = v.x * sinTheta + v.y * cosTheta;

            return new Vector2(newX, newY);
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static Vector2 Vec3ToVec2(Vector3 vec)
        {
            return new Vector2(vec.x, vec.y);
        }
    }
}